package net.onceface.web.resources;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import net.onceface.application.CartApplication;
import net.onceface.application.commons.InvokeResult;
import net.onceface.application.commons.Message;

/**
 * 购物车资源类
 * **/
@Path("/cart")
public class CartResource extends BaseResource{

	@Inject 
	private CartApplication application;
	
	/**
	 * 按用户ID和折扣ID重新计算价格
	 * **/
	@GET
	@Path("/getCartPrice")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public InvokeResult getCartPrice(@QueryParam("clientId")final Long clientId, 
								@QueryParam("disCountId")final Long disCountId){
		try{
			return InvokeResult.success(application.getCartPrice(clientId, disCountId));
		}catch(Exception e){
			return InvokeResult.failure(Message.GET_CART_FAILURE);
		}
	}
	
	/**
	 * 按用户ID获取购物车
	 * **/
	@GET
	@Path("/getCart")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public String getCart(@QueryParam("clientId")final Long clientId){
		try{
			return toJson(InvokeResult.success(application.getCartDetails(clientId)));
		}catch(Exception e){
			return toJson(InvokeResult.failure(Message.GET_CART_FAILURE));
		}
	}
	
	/**
	 * 加入商品到购物车
	 * **/
	@POST
	@Path("/add")
	@Consumes({MediaType.APPLICATION_FORM_URLENCODED,MediaType.APPLICATION_JSON})
	@Produces(MediaType.APPLICATION_JSON)
	public String addCart(@FormParam("clientId") final Long clientId,
								@FormParam("specId") final Long specId, 
								@FormParam("productId") final Long productId,
								@FormParam("quantity") final int quantity){
		try{
			return toJson(application.addProduct(clientId, specId, productId, quantity));
		}catch(Exception e){
			return toJson(InvokeResult.failure(Message.ADD_CART_FAILURE));
		}
		
	}
	
	@POST
	@Path("/remove")
	@Consumes({MediaType.APPLICATION_FORM_URLENCODED,MediaType.APPLICATION_JSON})
	@Produces(MediaType.APPLICATION_JSON)
	public String remove(@FormParam("clientId")final Long clientId, @FormParam("id")final Long id){
		try{
			return toJson(application.remove(clientId, id));
		}catch(Exception e){
			return toJson(InvokeResult.failure(Message.REMOVE_CART_FAILURE));
		}
	}
	
	@POST
	@Path("/edit")
	@Consumes({MediaType.APPLICATION_FORM_URLENCODED,MediaType.APPLICATION_JSON})
	@Produces(MediaType.APPLICATION_JSON)
	public String edit(@FormParam("clientId")final Long clientId, 
							 @FormParam("id")final Long id, 
							 @FormParam("quantity")final int quantity){
		
		try{
			return toJson(application.editQuantity(clientId, id, quantity));
		}catch(Exception e){
			return toJson(InvokeResult.failure(Message.EDIT_CART_FAILURE));
		}
	}
	
	@POST
	@Path("/checked")
	@Consumes({MediaType.APPLICATION_FORM_URLENCODED,MediaType.APPLICATION_JSON})
	@Produces(MediaType.APPLICATION_JSON)
	public String checked(@FormParam("clientId")final Long clientId, @FormParam("id")final Long id){
		try{
			return toJson(application.checked(clientId, id));
		}catch(Exception e){
			return toJson(InvokeResult.failure(Message.APPLICATION_FAILURE));
		}
	}
	
	@POST
	@Path("/checkedAll")
	@Consumes({MediaType.APPLICATION_FORM_URLENCODED,MediaType.APPLICATION_JSON})
	@Produces(MediaType.APPLICATION_JSON)
	public String checked(@FormParam("clientId")final Long clientId, @FormParam("check")boolean check){
		try{
			return toJson(application.checkedAll(clientId, check));
		}catch(Exception e){
			return toJson(InvokeResult.failure(Message.APPLICATION_FAILURE));
		}
	}
	
}
